<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>BOOKS</title>

    <!-- Bootstrap core CSS -->
    <link href="/static/css/bootstrap.min.css" rel="stylesheet">
    <!-- Custom styles for this template -->
    <link href="/static/css/dashboard.css" rel="stylesheet">
    <link rel="stylesheet" href="/static/css/font-awesome.min.css">
</head>

<body>

    <nav class="navbar navbar-inverse navbar-fixed-top">
        <div class="container-fluid">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar"
                        aria-expanded="false" aria-controls="navbar">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#">图书管理系统</a>
            </div>
            <div id="navbar" class="navbar-collapse collapse">
                <ul class="nav navbar-nav navbar-right">
                    <li><a href="#">Admin</a></li>
                </ul>
            </div>
        </div>
    </nav>
    <div class="container-fluid">
    <div class="row">
        <div class="col-sm-3 col-md-2 sidebar">
            <ul class="nav nav-sidebar">
                <li class="active"><a href="#">主页<span class="sr-only">(current)</span></a></li>
            </ul>
        </div>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
            <h2 class="page-header">图书列表</h2>

            <button class="btn btn-success btn-sm" data-toggle="modal" data-target="#add-book" id="addBook"><i
                    class="fa fa-plus-square-o"> 添加书籍</i></button>
            <div class="table-responsive" style="padding-top: 10px;">
                <table class="table table-striped table-bordered table-hover">
                    <thead>
                    <tr>
                        <th style="text-align: center;">序号</th>
                        <th style="text-align: center;">书籍名称</th>
                        <th style="text-align: center;">价格</th>
                        <th style="text-align: center;">出版日期</th>
                        <th style="text-align: center;">出版社</th>
                        <th style="text-align: center;">操作</th>
                    </tr>
                    </thead>
                    <tbody style="text-align: center;">
                    {% for book in book_list %}
                        <tr>
                            <td target="forloop">{{ forloop.counter }}</td>
                            <td class="hidden" target="bid">{{ book.id }}</td>
                            <td target="title">{{ book.title }}</td>
                            <td target="price">{{ book.price }}</td>
                            <td target="pub_date">{{ book.pub_date|date:"Y-m-d" }}</td>
                            <td target="publish">{{ book.publish }}</td>
                            <td style="max-width: 80px;">
                                <button class="btn btn-primary btn-xs" data-toggle="modal" data-target="#edit-book"><i class="fa fa-edit edit-book"> 修改</i></button>
                                <button href="{% url 'del_book' %}" bid="{{ book.id }}" class="btn btn-danger btn-xs delBook"><i class="fa fa-trash-o"> 删除</i></button>
                            </td>
                        </tr>
                    {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" tabindex="10" role="dialog" aria-labelledby="gridSystemModalLabel" id="add-book">
    <div class="modal-dialog" role="document">
        <form class="form-horizontal" action="{% url 'add_book' %}" method="post">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span>
                    </button>
                    <h4 class="modal-title">添加书籍</h4>
                </div>
                <div class="modal-body addBook">
                    <div class="row">
                        <div class="col-md-10">
                            <div class="form-group">
                                <label for="book-name" class="col-sm-4 control-label">书籍名称:</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control" id="book-name" placeholder="书籍名称" name="title">
                                </div>
                            </div>
                        </div>

                        <div class="col-md-10">
                            <div class="form-group">
                                <label for="book-price" class="col-sm-4 control-label">书籍价格:</label>
                                <div class="col-sm-8">
                                    <input type="number" class="form-control" id="book-price" name="price" placeholder="书籍价格" min="1" max="1000">
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-10">
                            <div class="form-group">
                                <label for="pub-date" class="col-sm-4 control-label">出版日期:</label>
                                <div class="col-sm-8">
                                    <input type="date" class="form-control" name="pub_date" id="pub-date" placeholder="出版日期">
                                </div>
                            </div>
                        </div>

                        <div class="col-md-10">
                            <div class="form-group">
                                <label for="publish" class="col-sm-4 control-label">出版社:</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control" name="publish" id="publish" placeholder="出版社">
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-primary">保存</button>
                </div>
            </div>
        </form>
    </div>
</div>


<div class="modal fade" tabindex="12" role="dialog" aria-labelledby="gridSystemModalLabel" id="edit-book">
    <div class="modal-dialog" role="document">
        <form class="form-horizontal" action="{% url 'edit_book' %}" method="post">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span>
                    </button>
                    <h4 class="modal-title">修改书籍</h4>
                </div>
                <div class="modal-body editBook">
                    <div class="row">
                        <div class="col-md-10 hidden">
                            <div class="form-group">
                                <label for="edit-book-id" class="col-sm-4 control-label">ID:</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control" id="edit-book-id" placeholder="ID" name="bid">
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-10">
                            <div class="form-group">
                                <label for="edit-book-name" class="col-sm-4 control-label">书籍名称:</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control" id="edit-book-name" placeholder="书籍名称" name="title">
                                </div>
                            </div>
                        </div>

                        <div class="col-md-10">
                            <div class="form-group">
                                <label for="edit-book-price" class="col-sm-4 control-label">书籍价格:</label>
                                <div class="col-sm-8">
                                    <input type="number" class="form-control" id="edit-book-price" name="price" placeholder="书籍价格" min="1" max="1000">
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-10">
                            <div class="form-group">
                                <label for="edit-pub-date" class="col-sm-4 control-label">出版日期:</label>
                                <div class="col-sm-8">
                                    <input type="date" class="form-control" name="pub_date" id="edit-pub-date" placeholder="出版日期">
                                </div>
                            </div>
                        </div>

                        <div class="col-md-10">
                            <div class="form-group">
                                <label for="edit-publish" class="col-sm-4 control-label">出版社:</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control" name="publish" id="edit-publish" placeholder="出版社">
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-primary">保存</button>
                </div>
            </div><!-- /.modal-content -->
        </form>
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->


    <script src="/static/js/jquery-1.12.4.js"></script>
    <script src="/static/js/bootstrap.min.js"></script>
    <script src="https://cdn.bootcss.com/limonte-sweetalert2/7.21.1/sweetalert2.all.min.js"></script>
    <script>
        $('.edit-book').click(function () {
            var tds = $(this).parent().parent().prevAll();
            tds.each(function () {
                var attr = $(this).attr('target');
                if(attr!="forloop"){
                    var text = $(this).text();
                    $('.editBook input[name=' + attr + ']').val(text);
                }
            });
        });
        $('#addBook').click(function () {
            $('.addBook input').val('');
        });

        $('.delBook').click(function () {
            var current_lable = $(this);
            swal({
                title: '确定删除吗？',
                text: '删除后将无法恢复！！！',
                type: 'warning',
                showCancelButton: true,
                showCloseButton: true,
                confirmButtonColor: '#d33',
                cancelButtonColor: '#3085d6',
                confirmButtonText: '确定',
                cancelButtonText: '取消',
            }).then(function (isConfirm) {
                if(isConfirm['value']){
                    $.ajax({
                        url: current_lable.attr('href'),
                        type: 'POST',
                        dataType: 'JSON',
                        data: {"bid": current_lable.attr('bid')},
                        success: function (data) {
                            if(data['status']){
                                swal(
                                    '删除成功!',
                                    '你的数据已删除...',
                                    'success'
                                );
                                current_lable.parent().parent().remove();
                            }else {
                                swal(
                                    '删除失败!',
                                    '你要删除的数据不存在...',
                                    'warning'
                                )
                            }
                        }
                    });
                }
            });
        })


    </script>

</body>
</html>